package com.xrz.sxm.aj.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ProgressBar;

import java.util.Timer;
import java.util.TimerTask;

public class Activity_Loading extends Activity {

	private TimerTask m_TimerTask;
	private Timer m_Timer;

	public static boolean bShow = false;
	
	private ProgressBar m_ProgressWheel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_loading);
		initData();
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			return false;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}





	protected void initData() {
		m_ProgressWheel = (ProgressBar) findViewById(R.id.loading_img);
		
		m_TimerTask = new TimerTask() {
			public void run() {
				if (bShow == false) {
					cancel();
					finish();
				}else{
				}
			}
		};
		m_Timer = new Timer(true);
		m_Timer.schedule(m_TimerTask, 500, 500);
	}
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {
			return true;
		}
		return super.onTouchEvent(event);
	}

	private boolean isOutOfBounds(Activity context, MotionEvent event) {
		final int x = (int) event.getX();
		final int y = (int) event.getY();
		final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
		final View decorView = context.getWindow().getDecorView();
		return (x < -slop) || (y < -slop)|| (x > (decorView.getWidth() + slop))|| (y > (decorView.getHeight() + slop));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (m_TimerTask != null)
			m_TimerTask.cancel();
		if (m_Timer != null)
			m_Timer.cancel();
	}

}
